package com.hamu.controller;

import com.hamu.pojo.Article;
import com.hamu.pojo.PageBean;
import com.hamu.pojo.Tag;
import com.hamu.pojo.Result;
import com.hamu.service.TagService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    @PostMapping
    public Result add(@RequestBody @Validated(Tag.Add.class) Tag tag) {
        tagService.add(tag);
        return Result.success();
    }

    @GetMapping
    public Result<List<Tag>> list() {
        List<Tag> tags = tagService.list();
        return Result.success(tags);
    }

    @GetMapping("/all")
    public Result<List<Tag>> listAll() {
        List<Tag> tags = tagService.listAll();
        return Result.success(tags);
    }

    @GetMapping("/detail")
    public Result<Tag> detail(@RequestParam Integer tagId) {
        Tag tag = tagService.findById(tagId);
        return Result.success(tag);
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Tag.Update.class) Tag tag) {
        tagService.update(tag);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@NotNull @RequestParam Integer tagId) {
        tagService.delete(tagId);
        return Result.success();
    }

    //搜索tag
    @GetMapping("/search")
    public Result<PageBean<Tag>> searchTags(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String query){
        PageBean<Tag> pb = tagService.searchList(pageNum, pageSize,query);
        return Result.success(pb);
    }

}